package com.cecsmsserve.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cecsmsserve.entity.HealthStatisticConfig;
import com.cecsmsserve.mapper.HealthStatisticConfigMapper;
import com.cecsmsserve.service.HealthStatisticConfigService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HealthStatisticConfigServiceImpl extends ServiceImpl<HealthStatisticConfigMapper, HealthStatisticConfig> implements HealthStatisticConfigService {
    
    @Override
    public List<HealthStatisticConfig> getVisibleConfigsByUserId(Integer userId) {
        if (userId == null) {
            return null;
        }
        
        QueryWrapper<HealthStatisticConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
              .eq("is_visible", true)
              .orderByAsc("display_order");
        return list(wrapper);
    }
    
    @Override
    public boolean saveUserConfig(Integer userId, List<HealthStatisticConfig> configs) {
        if (userId == null || configs == null) {
            return false;
        }
        
        // 先删除用户原有的配置
        QueryWrapper<HealthStatisticConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        remove(wrapper);
        
        // 添加新的配置
        for (HealthStatisticConfig config : configs) {
            config.setUserId(userId);
            save(config);
        }
        
        return true;
    }
    
    @Override
    public List<HealthStatisticConfig> getAllConfigsByUserId(Integer userId) {
        if (userId == null) {
            return null;
        }
        
        QueryWrapper<HealthStatisticConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId)
              .orderByAsc("display_order");
        return list(wrapper);
    }
    
    @Override
    public boolean updateConfigVisibility(Integer id, Integer userId, Boolean isVisible) {
        if (id == null || userId == null || isVisible == null) {
            return false;
        }
        
        HealthStatisticConfig config = new HealthStatisticConfig();
        config.setId(id);
        config.setIsVisible(isVisible);
        
        QueryWrapper<HealthStatisticConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id)
              .eq("user_id", userId);
        
        return update(config, wrapper);
    }
    
    @Override
    public boolean updateConfigOrder(Integer id, Integer userId, Integer displayOrder) {
        if (id == null || userId == null || displayOrder == null) {
            return false;
        }
        
        HealthStatisticConfig config = new HealthStatisticConfig();
        config.setId(id);
        config.setDisplayOrder(displayOrder);
        
        QueryWrapper<HealthStatisticConfig> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id)
              .eq("user_id", userId);
        
        return update(config, wrapper);
    }
} 